package s47.dqj.dao;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import s47.dqj.bean.DepartmentLogin;

/**
 * 5.科室人员登录表
 * @author Administrator
 *
 */
@Repository("departmentLoginDaoImpl")
public class DepartmentLoginDaoImpl extends HibernateDaoSupport implements DepartmentLoginDaoInf{
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentLoginDaoInf#setSupperSessionFactory(org.hibernate.SessionFactory)
	 */
	@Override
	@Resource(name="sessionFactory")
	public void setSupperSessionFactory(SessionFactory sessionFactory){
		super.setSessionFactory(sessionFactory);
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentLoginDaoInf#findAllByCondition(java.lang.String, java.lang.Integer)
	 */
	@Override
	public List<DepartmentLogin> findAllByCondition(String hql,Integer pageNum){
		Query query=getSession().createQuery(hql);
		query.setFirstResult((Integer.valueOf(pageNum)-1)*10);
		query.setMaxResults(10);
		return query.list(); 
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentLoginDaoInf#selectPageTotal(java.lang.String)
	 */
	@Override
	public List<Integer> selectPageTotal(String tempHQL){
		List<Integer> pageTotal=new ArrayList<Integer>();
		int p;
		Long temp;
		String hql="select Count(*) from DepartmentLogin as dl"+tempHQL;
		Query query=getSession().createQuery(hql);
		temp=(Long)query.uniqueResult();
		
		if(temp%10!=0)
			p=(int)(temp/10+1);
		else
			p=(int)(temp/10);
		for(int n=0;n<p;n++){
			pageTotal.add(n+1);
		}
		return pageTotal;
	} 
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentLoginDaoInf#save(s47.dqj.bean.DepartmentLogin)
	 */
	@Override
	public void save(DepartmentLogin departmentLogin){
		getHibernateTemplate().save(departmentLogin);
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentLoginDaoInf#update(s47.dqj.bean.DepartmentLogin)
	 */
	@Override
	public void update(DepartmentLogin departmentLogin){
		//System.out.println(departmentLogin.getDlip()+"=========");
		getHibernateTemplate().update(departmentLogin);
		//System.out.println("finished==========");
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentLoginDaoInf#delete(s47.dqj.bean.DepartmentLogin)
	 */
	@Override
	public void delete(DepartmentLogin departmentLogin){
		getHibernateTemplate().delete(departmentLogin);
	}
	
	/* (non-Javadoc)
	 * @see s47.dqj.dao.DepartmentLoginDaoInf#findObject(s47.dqj.bean.DepartmentLogin)
	 */
	@Override
	public DepartmentLogin findObject(DepartmentLogin departmentLogin){
		//System.out.println("findObject============");
		return getHibernateTemplate().get(DepartmentLogin.class, departmentLogin.getDlid());
	}
	
	public DepartmentLogin findLogin(DepartmentLogin departmentLogin){
		String hql="from DepartmentLogin as dl where dl.dllogin=:dllogin and dl.dlpassword=:dlpassword";
		Query query=getSession().createQuery(hql);
		//System.out.println(departmentLogin.getDllogin()+"======");
		//System.out.println(departmentLogin.getDlpassword()+"====");
		query.setString("dllogin", departmentLogin.getDllogin());
		query.setString("dlpassword", departmentLogin.getDlpassword());
		return (DepartmentLogin)query.uniqueResult();
	}
}
